package com.sphd.emanu.activity.integralscore;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.activity.pointcommondity.PointCommondityActivity;
import com.sphd.emanu.activity.pointconsume.PointconsumeActivity;
import com.sphd.emanu.activity.pointrecharge.PointRechargeActivity;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.goods.Commodity;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.market.Points;
import com.sphd.emanu.pojo.market.PointsQueryVo;
import com.sphd.emanu.pojo.market.Rules;
import com.sphd.emanu.utils.PermissionConfig;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 营销模块---积分设置
 *
 * @Author Hu
 * @Time 2017/5/15 -- 10:49.
 * @Email 87683202@qq.com
 */
public class IntegralscoreActivity extends
        MVPBaseActivity<IntegralscoreContract.View, IntegralscorePresenter> implements IntegralscoreContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    Intent intent;
    Bundle bundle;
    Rules rules;
    User user;
    @BindView(R.id.tv_topup)
    TextView tvTopup;
    @BindView(R.id.tv_consume)
    TextView tvConsume;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    @BindView(R.id.layout_top_up)
    LinearLayout layoutTopUp;
    @BindView(R.id.layout_consume)
    LinearLayout layoutConsume;
    @BindView(R.id.layout_edit)
    LinearLayout layoutEdit;
    @BindView(R.id.point_show)
    LinearLayout pointShow;
    PointsQueryVo pointsQueryVo;
    View viewSwitch;
    Switch mtSwitch;
    Button btnSave, btnCancle;
    AlertDialog alertDialog;
    Points points2, points3;
    List<Moudle> moudles;
    boolean isCanDo = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integralscore);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.updateConfig(rules.getConfig().getId(), user.getOid(), mtSwitch.isChecked());

            }
        });
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });


    }

    private void initView() {
        setTitleBar(toolbar, "积分设置");
        intent = getIntent();
        bundle = intent.getExtras();
        rules = (Rules) bundle.getSerializable(Constant.RULES);
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudles, PermissionConfig.MENU_MAKETING_4);
        initDialog();
        addData();
    }

    private void initDialog() {
        viewSwitch = View.inflate(context, R.layout.dialog_switch, null);
        mtSwitch = (Switch) viewSwitch.findViewById(R.id.switch_edit_point_mt);
        btnSave = (Button) viewSwitch.findViewById(R.id.btn_save);
        btnCancle = (Button) viewSwitch.findViewById(R.id.btn_cancle);
        alertDialog = getDialogBuilder(viewSwitch, R.style.AlertDialog);

    }

    private void addData() {
        mPresenter.selectPoints(user.getOid());
    }

    @Override
    public void recBean(PointsQueryVo pointsQueryVo) {
        this.pointsQueryVo = pointsQueryVo;
        List<Points> pointses = pointsQueryVo.getPointsList();
        mtSwitch.setChecked(pointsQueryVo.getPiont());
        tvEdit.setText(pointsQueryVo.getPiont() ? "已开启" : "未开启");
        /*类型:1-充值,2-现金消费,3-充值消费',*/
        for (Points points : pointses) {
            if (points.getType().equals("1")) {
                tvTopup.setText(points.getEnable() ? "已开启" : "未开启");
            } else if (points.getType().equals("2")) {
                points2 = points;
            } else if (points.getType().equals("3")) {
                points3 = points;
            }
        }
        tvConsume.setText((points2.getEnable() || points3.getEnable()) ? "已开启" : "未开启");
    }

    @OnClick({R.id.layout_top_up, R.id.layout_consume, R.id.layout_edit, R.id.point_show})
    public void onViewClicked(View view) {
        bundle.putSerializable(Constant.POINTS_QUERY_VO, pointsQueryVo);
        intent.putExtras(bundle);
        switch (view.getId()) {
            case R.id.layout_top_up:
                intent.setClass(context, PointRechargeActivity.class);
                startActivityForResult(intent, Constant.REQUEST_CODE);
                break;
            case R.id.layout_consume:
                intent.setClass(context, PointconsumeActivity.class);
                startActivityForResult(intent, Constant.REQUEST_CODE);
                break;
            case R.id.layout_edit:
                if (isCanDo){
                    if (alertDialog != null)
                        if (!alertDialog.isShowing())
                            alertDialog.show();
                }else {
                    toast(getString(R.string.no_perssion));
                }

                break;
            case R.id.point_show:
                intent.setClass(context, PointCommondityActivity.class);
                startActivityForResult(intent, Constant.REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUEST_CODE) {
            addData();
        }
    }


    @Override
    public void recRes(int res) {
        if (res == 1) {
            toast(R.string.up_success);
            alertDialog.dismiss();
            addData();
        } else {
            toast(R.string.up_failure);
        }
    }
}
